﻿using System;
using System.Collections.Generic;
using System.Linq;
using ExpertApplication.DatabaseModel;
using ExpertApplication.ViewModels;
using Ninject;
using Ninject.Modules;

namespace ExpertApplication.DatabaseService
{
    public class ExpertContextManager : NinjectModule, IExpertContextManager
    {
        [Inject]
        private readonly ExpertContext _context;

        public ExpertContextManager(ExpertContext context)
        {
            _context = context;
        }

        #region IExpertContextManager Members

        public List<QuestionViewModel> GetQuesitonForLevel(int level)
        {
            var questions = from q in _context.Questions
                            where q.Level.Level == level
                            select new QuestionViewModel
                                       {
                                           Id = q.Id,
                                           IsMultiSelected = q.IsMultiSelected,
                                           Text = q.Text,
                                           AnswerViewModels = (from a in q.Answers
                                                               select new AnswerViewModel
                                                                          {
                                                                              Text = a.Text,
                                                                              Id = a.Id,
                                                                              Checked = false
                                                                          }).ToList()
                                       };
            return questions.ToList();
        }

        public List<QuestionViewModel> GetQuestionForLevel(QuestionLevel level)
        {
            return GetQuesitonForLevel(level.Level);
        }

        public void AddUser(User user)
        {
            if(user == null) throw new ArgumentNullException("user");
            var userSearch = _context.Users.Find(user.Uid);
            if(userSearch == null)
            {
                user.IsLogged = true;
                _context.Users.Add(user);
            }
            else
            {
                if(user.Hash != userSearch.Hash)
                {
                    _context.Users.Remove(user);
                }
            }
            _context.SaveChanges();

        }

        public float GetPercentForLevel(IEnumerable<QuestionViewModel> answers, int level)
        {
            if(answers == null) throw new ArgumentNullException("answers");
            var res1 = from ans in _context.Answers
                       where ans.Question.IsMultiSelected == false && ans.Question.Level.Level == level
                       select new { Sum = ans.Question.Weight * ans.Weight };
            var maxForOneVariantAQuestion = res1.Sum(s => s.Sum);
            var res2 = from ans in _context.Answers
                       where ans.Question.IsMultiSelected && ans.Question.Level.Level == level && ans.Weight >= 0
                       select new { Sum = ans.Question.Weight * ans.Weight };
            var maxForMultiVariantQuesiton = res2.Sum(s => s.Sum);
            var totalmaxMark = maxForMultiVariantQuesiton + maxForOneVariantAQuestion;

            const float myresult = 25f;


            return (myresult / totalmaxMark) * 100;
        }

        #endregion

        public override void Load()
        {
            Bind<ExpertContext>().To<ExpertContext>();
        }
    }
}